package xujiwen.transport.Mgr;


import java.util.ArrayList;
import java.util.List;

import xujiwen.transport.bean.Owner;
import xujiwen.transport.util.DBConnect;

public class OwnerMgr {
	public void Save(final Owner owner) {

		final DBConnect db = new DBConnect();
		final String sql = "insert into CAROWNER values (?, ?, ?, ?)";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			final String ownerId = owner.getOwnerId();
			final String name = owner.getName();
			final String phone = owner.getPhone();
			
			final String sex = owner.getSex();
			db.setString(1, ownerId);
			db.setString(2, name);
			db.setString(3, phone);
			db.setString(4, sex);
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	public Owner GetDeatilUser(final int oid) {
		Owner owner = null;
		final DBConnect db = new DBConnect();
		final String sql = "select * from CAROWNER where OWNERID = ?";
		db.prepareStatement(sql);
		db.setInt(1, oid);
		db.excuteQuery();
		try {
			while (db.next()) {
				owner = new Owner();
				owner.setOwnerId(db.getString("OWNERID"));
				owner.setName(db.getString("NAME"));
				owner.setPhone(db.getString("PHONE"));
				owner.setSex(db.getString("SEX"));
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return owner;
	}
	
	public void Update(final Owner owner) {
		final DBConnect db = new DBConnect();

		final String sql = "update CAROWNER set "
				+ "NAME=?, PHONE=?, SEX=? "
				+ "where OWNERID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			final String name = owner.getName();
			final String phone = owner.getPhone();
			final String sex = owner.getSex();
			final String ownerId = owner.getOwnerId();

			db.setString(1, name);
			db.setString(2, phone);
			db.setString(3, sex);
			db.setString(4, ownerId);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	public List<Owner> GetSearchUserList(String str) {
		final List<Owner> owners = new ArrayList<Owner>();
		final DBConnect db = new DBConnect();
		final String sql;
		if(str.length() > 0)
		sql = "select * from CAROWNER where NAME like '%" + str +"%' or OWNERID like '%" + str +"%'";
		else sql = "select * from CAROWNER limit 10";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final Owner owner = new Owner();
				owner.setOwnerId(db.getString("OWNERID"));
				owner.setName(db.getString("NAME"));
				owner.setPhone(db.getString("PHONE"));
				owner.setSex(db.getString("SEX"));
				owners.add(owner);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return owners;
	}
	
	public void DelUser(final String oid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from CAROWNER where OWNERID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, oid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
}
